﻿using System;
using System.Net.Http;
using System.Threading.Tasks;
using VupSlashLauncher.Common;

namespace VupSlashLauncher.Model
{
    public static class INet
    {

        //古老的HttpResponse方法
        //public static string GetHttpResponse(string url)
        //{
        //    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        //    request.Method = "GET";
        //    request.ContentType = "text/html;charset=UTF-8";
        //    request.UserAgent = null;
        //    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        //    Stream myResponseStream = response.GetResponseStream();
        //    StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
        //    string retString = myStreamReader.ReadToEnd();
        //    myStreamReader.Close();
        //    myResponseStream.Close();
        //    return retString;
        //}

        public static async Task ReportPatchDownloadTimesPlus()
        {
            using (HttpClient client = new HttpClient())
            {
                try
                {
                    client.Timeout = TimeSpan.FromSeconds(30);
                    HttpResponseMessage response = await client.GetAsync(GlobalValues.CountUrl + "?patch" + GlobalValues.LatestVersion);
                }
                catch { GlobalValues.HaveNet = false; }
            }
        }
        public static async Task ReportMainDownloadTimesPlus()
        {
            using (HttpClient client = new HttpClient())
            {
                try
                {
                    client.Timeout = TimeSpan.FromSeconds(30);
                    HttpResponseMessage response = await client.GetAsync(GlobalValues.CountUrl + "?main" + GlobalValues.LatestVersion);
                }
                catch { GlobalValues.HaveNet = false; }
            }
        }
        public static async Task GetInformation()
        {
            using (HttpClient client = new HttpClient())
            {
                try
                {
                    client.Timeout = TimeSpan.FromSeconds(30);
                    HttpResponseMessage response = await client.GetAsync(GlobalValues.UpdateUrl);
                    response.EnsureSuccessStatusCode();
                    string temp = await response.Content.ReadAsStringAsync();
                    string[] info = temp.Split(new string[] { "\r\n" }, StringSplitOptions.None);
                    try
                    {
                        if ("vupslash" == info[0]) GlobalValues.HaveNet = true;
                        GlobalValues.OfficialUrl = info[1];
                        GlobalValues.LatestLauncherVersion = info[2];
                    }
                    catch
                    {
                        GlobalValues.HaveNet = false;
                    }
                }
                catch { GlobalValues.HaveNet = false; }
            }
        }
        public static async Task GetNotice()
        {
            using (HttpClient client = new HttpClient())
            {
                try
                {
                    client.Timeout = TimeSpan.FromSeconds(30);
                    HttpResponseMessage response = await client.GetAsync(GlobalValues.GetNoticeUrl);
                    response.EnsureSuccessStatusCode();
                    string temp = await response.Content.ReadAsStringAsync();
                    string[] info = temp.Split(new string[] { "\r\n" }, StringSplitOptions.None);
                    try
                    {
                        GlobalValues.NoticeLatest = info[0];
                        GlobalValues.NoticeUrl = info[1];
                        for (int i = 2; i < info.Length; i++) GlobalValues.NoticeContent += info[i] + "\n";
                    }
                    catch
                    {
                        GlobalValues.NoticeLatest = null;
                        GlobalValues.NoticeUrl = null;
                    }
                }
                catch { GlobalValues.HaveNet = false; }
            }
        }
        public static async Task GetVersionCSV()
        {
            using (HttpClient client = new HttpClient())
            {
                try
                {
                    client.Timeout = TimeSpan.FromSeconds(30);
                    HttpResponseMessage response = await client.GetAsync(GlobalValues.GetVersionUrl);
                    response.EnsureSuccessStatusCode();
                    string temp = await response.Content.ReadAsStringAsync();
                    GlobalValues.VersionList = IFunc.ReadCSV(await response.Content.ReadAsStringAsync());
                    GlobalValues.LatestVersion = GlobalValues.VersionList[GlobalValues.VersionList.Length - 1][1];
                    GlobalValues.DownloadPatchUrl = GlobalValues.VersionList[GlobalValues.VersionList.Length - 1][4];
                    if (GlobalValues.LatestVersion == null || GlobalValues.LatestVersion == "") GlobalValues.LatestVersion = "未知版本";
                }
                catch { GlobalValues.HaveNet = false; }
            }
        }

    }
}
